import {saveGoodsToLive} from '../../../../utils/imapi'
var app = getApp();
Component({
    properties: {
        productList: {
            type:Array,
            value:[],
            observer:function(newVal, oldVal){
                if(!oldVal[0])return;
                for(var i = 0;i<oldVal.length;i++){
                    if(newVal[i] && oldVal[i] && newVal[i].item_id==oldVal[i].item_id){
                        newVal[i] = oldVal[i]
                    }
                }
                this.setData({
                  productList:newVal
                })
            }
        },
        showTitle:Boolean,
        bgColor:String,
        isLive:String,
        showList:Boolean,
        is_select:{
            type: [Boolean, String],
            default: false
        },
		is_bigin:{
			type: Boolean,
			value: true
		},
    },
    data: {
        productList: [],
        
    },
    ready() {
        
    },
    methods: {
        //商品详情
        productDetail(e) {
			console.log(e);
            if(this.long_click){
                this.long_click = false
                return
            } 
            var id = e.currentTarget.dataset.id;
            var p = e.currentTarget.dataset.p;
            var index = e.currentTarget.dataset.index;
            app.globalData.cur_product = this.data.productList[index];
            wx.navigateTo({
                url: '/pages/module/productDetail/productDetail?id=' + id + '&p=' + p,
            })
        },
        chooseProduct(e){
			console.log(e);
            var index = e.currentTarget.dataset.index;
            this.triggerEvent('change', {index: e.currentTarget.dataset.index});
        },
        imgLoad(e){
            var index = e.currentTarget.dataset.index;
            this.data.productList[index].imgShow=true;
            this.setData({
                productList:this.data.productList
            })
        },
        goHot(e){
            var index = e.currentTarget.dataset.index;var index = e.currentTarget.dataset.index;
            this.data.productList[index].is_show_mask=false;
            this.setData({
                productList:this.data.productList
            })
            wx.navigateTo({
              url: '/pages/module/hot/hot?q=' + this.data.productList[index].cid_name,
            })
        },
        showMask(e){
			//console.log(e);
            this.long_click = true;
            var index = e.currentTarget.dataset.index;
            this.data.productList.forEach(item=>{
                item.is_show_mask=false;
            })
            this.data.productList[index].is_show_mask=true;
            this.setData({
                productList:this.data.productList
            })
        },
        hideMask(e){
			console.log(e);
            if(this.long_click){
                this.long_click = false
                return
            } 
            var index = e.currentTarget.dataset.index;
            this.data.productList[index].is_show_mask=false;
            this.setData({
                productList:this.data.productList
            })
        },
        add_live(e) {
            const item = e.currentTarget.dataset.item
            const p = item.p
            const item_id = item.item_id
            const title = item.title
            const tip = '把'+p+'上'+title+'添加到直播间吗?'
            wx.showModal({
                title:'加入直播间',
                content:tip,
                success:async function(res) {
                    if(res.confirm) {
                        try {
                            wx.showLoading({
                                icon:'none',
                                title:'正在添加商品，请稍后'
                               })
                            // 调用添加到g_goods 并提审
                         const data =await saveGoodsToLive({
                           p:p,
                           item_id:item_id
                         })
     
                         console.log(data);
                         //  确定加入
                         wx.showToast({
                           icon:'none',
                             title: '5分钟后 商品管理查看审核状态',
                         })
                         wx.hideLoading({})
                         return ;
                         }catch(err) {
                           wx.showToast({
                             icon:'none',
                             title:'商品未添加成功'
                           })
                         }
                        
     
                    }else {
                        wx.showToast({
                            title:'再看看其他宝贝',
                        })
                    }
                }
            })
        }
    },
})